package com.lanyam.travel.base.service.impl;


import com.lanyam.travel.base.entity.Dictionary;
import com.lanyam.travel.base.entity.vo.DictionaryVo;
import com.lanyam.travel.base.dao.DictionaryMapper;
import com.lanyam.travel.base.service.IDictionaryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;


@Service
@Transactional
@Slf4j
public class DictionaryServiceImpl implements IDictionaryService {

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    public IPage<DictionaryVo> getPageDictionary (Map<String, Object> queryMap, Integer page, Integer rows) {
        Page<DictionaryVo> pageSize = new Page<DictionaryVo>(page, rows);
        Page<DictionaryVo> pageInfo = dictionaryMapper.getPage(pageSize, queryMap);
        return pageInfo;
    }

    @Override
    public void addDictionary(Dictionary dictionary) {
        int count = dictionaryMapper.getCountByDictCode(dictionary.getDictCode());
        if (count > 0)
            throw new RuntimeException("字典编号不能重复！");
        dictionaryMapper.insert(dictionary);
    }

    @Override
    public void updateDictionary(Dictionary dictionary) {
        Dictionary dictInDB = dictionaryMapper.selectById(dictionary.getId());
        String dictCode = dictionary.getDictCode();
        String dictCodeInDB = dictInDB.getDictCode();
        if (dictCode == null)
            throw new RuntimeException("字典编码不能为空！");
        if (dictCodeInDB != null) {
            if (!dictCodeInDB.equals(dictCode)) {
                int count = dictionaryMapper.getCountByDictCode(dictCode);
                if (count > 0)
                    throw new RuntimeException("字典编号不能重复！");
            }
        } else {
            int count = dictionaryMapper.getCountByDictCode(dictCode);
            if (count > 0)
                throw new RuntimeException("字典编号不能重复！");
        }
        dictionaryMapper.updateById(dictionary);
    }

    @Override
    public void delById(String id) {
        dictionaryMapper.delById(id);
    }

    @Override
    public List<DictionaryVo> getListByType(String dictType) {
        List<DictionaryVo> listByType = dictionaryMapper.getListByType(dictType);
        return listByType;
    }

    @Override
    public List<Dictionary> getListBySuperId(String superId,String dictKey) {
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("super_id",superId);
        columnMap.put("dict_key",dictKey);
        List<Dictionary> listBySuperId = this.dictionaryMapper.selectByMap(columnMap);
        return listBySuperId;
    }
}